﻿@using Sharp.DataTransferObject
@using Sharp.Infrastructure.WebApiClient
@using Sharp.Portal.ExtNet
@using Sharp.ServiceContracts
@{
    Layout = null;
    var x = Html.X();

    var treeRoot = new Node()
    {
        Text = ".",
        NodeID = "Root",
        Expanded = true,
        AutoDataBind = false,
        Checked = null
    };
    IEnumerable<OrgDTO> list;
    using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
    {
        list = proxy.Channel.GetOrgs().Result;
    }
    var builder = new TreeBuilder<Guid, OrgDTO>
    {
        ChangeData = (p, treeNode) =>
        {
            treeNode.Text = p.Title;
            treeNode.NodeID = p.Id.ToString();
            treeNode.AutoDataBind = false;
            treeNode.Leaf = list.All(l => l.ParentId != p.Id);
            treeNode.Expanded = false;
            treeNode.Checked = false;
        }
    };

    builder.Build(list, treeRoot, null);
    if (treeRoot.Children.Count > 0)
    {
        treeRoot.Children[0].Expanded = true;
    }
}


<script>

    function treeclick(node, checkStatus, opts) {
        var centerContainer = opts.view.panel.ownerCt.down("container[region='center']");
        var ids = App.Grid.GetTreeValues(opts.view.panel);
        var grid = centerContainer.down("grid");
        if (grid) {
            var store = grid.getStore();
            if (ids.length > 0)
                store.proxy.extraParams["OrgId[]"] = ids;
            else
                store.proxy.extraParams["OrgId[]"] = [];
            store.load();
        } else {
            var form = centerContainer.down("form");

            var params = { containerId: form.ownerCt.id };

            Ext.net.directRequest({
                cleanRequest: true,
                url: form.tag.RefreshUrl,
                extraParams: params,
                method: "GET",
                @*eventMask: {
                    showMask: true,
                    msg: "正在刷新..."
                },*@
                control: this
            });
        }
    }
</script>

@(
        x.Container().Layout(LayoutType.Border).Items(
            x.TreePanel()
                .Title("组织")
                .Region(Region.West)
                .Layout(LayoutType.Fit)
                .Width(225)
                .MinWidth(225)
                .MaxWidth(400)
                .Split(true)
                .Collapsible(false).RootVisible(false).HideHeaders(true).Animate(false)
                .Root(treeRoot)
                .ViewConfig(x.TreeView().LoadMask(false))
                .Listeners(l => l.CheckChange.Fn = "treeclick"),

            x.Container().Layout(LayoutType.Fit).Region(Region.Center)
            .ItemsFromSection(this, "tabContent", true)

        )
)